package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gec.model.entity.SysEstateCount;
import com.gec.model.vo.*;
import com.gec.system.mapper.SysEstateCountMapper;
import com.gec.system.service.SysEstateCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.system.service.SysMenuService;
import com.gec.system.util.MD5Helper;
import com.gec.system.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统账号表 服务实现类
 * </p>
 *
 * @author lys
 * @since 2023-12-06
 */
@Service
@Transactional
public class SysEstateCountServiceImpl extends ServiceImpl<SysEstateCountMapper, SysEstateCount> implements SysEstateCountService {

    @Autowired
    private SysEstateCountMapper sysEstateCountMapper;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public IPage<SysEstateCount> selectPage(IPage<SysEstateCount> page1, SysEstateCountQueryVo vo) {
        return this.getBaseMapper().selectPage(page1, vo);
    }

    //根据登录名或者手机号查询用户
    @Override
    public SysEstateCount getUserInfoUserName(String loginName, String telephone) {

        SysEstateCount sysEstateCount = this.sysEstateCountMapper.getUserInfoUserName(loginName,telephone);
        return sysEstateCount;
    }

    @Override
    public SysEstateCount getCountInfoUserName(String loginName) {


        QueryWrapper<SysEstateCount> queryWrapper= new QueryWrapper<SysEstateCount>();
        queryWrapper.eq("login_name", loginName);
        SysEstateCount sysEstateCount = this.baseMapper.selectOne(queryWrapper);
        return sysEstateCount;
    }

    @Override
    public Map<String, Object> getCountInfo(String username) {

        Map<String, Object> map = new HashMap<>();

        //a.用户的基本信息
        SysEstateCount sysEstateCount = this.getCountInfoUserName(username);

        //当前权限控制使用不到，我们暂时忽略


        map.put("name", username);
        map.put("avatar", "https://img0.baidu.com/it/u=3359792453,190873072&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800");
        map.put("roles",  "[admin]");

        //b.根据账号id获取菜单权限值
        List<RouterVo> routerVoList =sysMenuService.findCountMenuList(sysEstateCount.getId());

        //c.根据用户id获取用户按钮权限
        List<String> countPermsList = sysMenuService.findCountPermsList(sysEstateCount.getId());

        map.put("buttons", countPermsList);
        map.put("routers", routerVoList);
        return map;
    }

    @Override
    public boolean updateByTel(SysEstateCount count) {

        System.out.println(count);
        String pass = count.getPassword();
        System.out.println("传到逻辑层的密码："+pass);

        String password = MD5Helper.encrypt(pass);
        System.out.println("加密后的密码"+password);
        String telephone = count.getTelephone();
        System.out.println("传到逻辑层的手机号："+telephone);


        QueryWrapper<SysEstateCount> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone", telephone);


        SysEstateCount sysEstateCount = this.sysEstateCountMapper.selectOne(queryWrapper);
        System.out.println("通过手机号查询出来的账号："+sysEstateCount);
        sysEstateCount.setPassword(password);
        int update = this.sysEstateCountMapper.update(sysEstateCount, queryWrapper);
        System.out.println("_____________________"+update);
        if (update==1){
            return true;
        }
        return false;


//        return this.sysEstateCountMapper.updateByTel(password,telephone);

    }



    @Override
    public Result compareAndUpdatePwd(UpdatePasswordVo vo) {


        SysEstateCount sysEstateCount = this.getBaseMapper().selectById(vo.getCountId());
        String pwdWithMD5 = MD5Helper.encrypt(vo.getPrePwd());

        System.out.println(pwdWithMD5);
        System.out.println(sysEstateCount.getPassword());
        if (!pwdWithMD5.equals(sysEstateCount.getPassword())){
            return Result.fail().message("原密码错误");
        }

        sysEstateCount.setPassword(MD5Helper.encrypt(vo.getNewPwd()));

        //修改密码
        this.getBaseMapper().updateById(sysEstateCount);
        return Result.ok();
    }

//    @Override
//    public boolean save(SysEstateCountSignInVo sysEstateCount) {
//
//        return false;
//    }


}
